package com.lzx.iteam.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.lzx.iteam.R;
import com.lzx.iteam.bean.LegworkRecordData;
import com.lzx.iteam.widget.gridimg.MyGridView;
import com.lzx.iteam.util.DateUtil;
import com.lzx.iteam.util.StringUtil;
import io.rong.imageloader.core.listener.ImageLoadingListener;
import io.rong.imageloader.core.listener.SimpleImageLoadingListener;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

import static com.lzx.iteam.util.Constants.dip2px;


public class LegworkRecordListAdapter extends BaseAdapter {

    private Context mContext;
    private ArrayList<LegworkRecordData> mRecordDatas;
    private ImageLoadingListener animateFirstListener;
    private EventImageAdapter mImageAdapter;
    public LegworkRecordListAdapter(Context mContext) {
        this.mContext = mContext;
        animateFirstListener = new AnimateFirstDisplayListener();

    }


    public void bindData(ArrayList<LegworkRecordData> recordDatas) {
        mRecordDatas = recordDatas;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return (mRecordDatas == null || mRecordDatas.size() == 0) ? 1 : mRecordDatas.size();
    }

    @Override
    public Object getItem(int position) {
        return (mRecordDatas == null || mRecordDatas.size() == 0) ? null : mRecordDatas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LegworkRecordData legworkRecordData = null;
        LegworkRecordData lastLegworkRecordData = null;
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.legwork_sign_item, null);
            holder.view1 = (ImageView) convertView.findViewById(R.id.legwork_sign_line_view_1);
            holder.view2 = (ImageView) convertView.findViewById(R.id.legwork_sign_line_view_2);
            holder.view3 = (ImageView) convertView.findViewById(R.id.legwork_sign_line_view_3);
            holder.view4 = (ImageView) convertView.findViewById(R.id.legwork_sign_line_view_4);

            holder.tvTime = (TextView) convertView.findViewById(R.id.legwork_sign_tv_time);
            holder.tvAddress = (TextView) convertView.findViewById(R.id.legwork_sign_tv_address);
            holder.lineLayout = (LinearLayout) convertView.findViewById(R.id.legwork_sign_line_layout);
            holder.gridView = (MyGridView) convertView.findViewById(R.id.legwork_sign_gv_image);

            convertView.setTag(holder);

        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        if(mRecordDatas==null||mRecordDatas.size()==0){
            holder.tvTime.setText("今日暂无签到");
            holder.tvAddress.setVisibility(View.GONE);
            holder.gridView.setVisibility(View.GONE);
        }else{
            legworkRecordData = mRecordDatas.get(position);
            // TODO 如果有两条数据,获取第一条数据
            if (getCount() == 2 && position == 1){
                lastLegworkRecordData = mRecordDatas.get(0);
            }
            holder.tvTime.setText("打卡时间"+ DateUtil.getHourAndMin(Long.parseLong(legworkRecordData.getTime())*1000));
            holder.tvAddress.setText(legworkRecordData.getAddress());
            try {
               if(!StringUtil.isEmpty( legworkRecordData.getAttachment())){
                   JSONArray jsonArray = new JSONArray(legworkRecordData.getAttachment());
                   if(jsonArray.length()>0){
                       holder.gridView.setVisibility(View.VISIBLE);
                       ArrayList<String> oriImages=new ArrayList<String>();
                       String[] thumbImages=new String[jsonArray.length()];
                       StringBuilder thumbImgs=new StringBuilder();
                       for (int i=0;i<jsonArray.length();i++){
                           JSONObject jsonObject=jsonArray.getJSONObject(i);
                           thumbImages[i]=jsonObject.getString("thumb");
                           oriImages.add(jsonObject.getString("ori"));
                           thumbImgs.append(jsonObject.getString("thumb")).append(",");
                       }
                       mImageAdapter = new EventImageAdapter(mContext,thumbImages,oriImages,thumbImgs.substring(0,thumbImgs.length()-1),legworkRecordData.getId());
                       holder.gridView.setAdapter(mImageAdapter);
                       // 防止Item不可点击
                       holder.gridView.setEnabled(false);
                       holder.gridView.setClickable(false);
                   }else{
                       holder.gridView.setVisibility(View.GONE);

                   }
               }else{
                   holder.gridView.setVisibility(View.GONE);
               }

            } catch (JSONException e) {
                e.printStackTrace();
            }
            holder.tvAddress.setVisibility(View.VISIBLE);
        }

        if (position == (getCount() - 1)) {
            //LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(dip2px(mContext, 30), ViewGroup.LayoutParams.WRAP_CONTENT);
            LinearLayout.LayoutParams params = null;
            /**
             * 三种情况
             * 1、有两条数据,都没有图片
             * 2、有多条数据,最后一条没有图片
             * 3、其他
             */
            if (getCount() > 1 && legworkRecordData != null && (StringUtil.isEmpty(legworkRecordData.getAttachment()))){
                if (lastLegworkRecordData != null && StringUtil.isEmpty(lastLegworkRecordData.getAttachment())){
                    params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, dip2px(mContext, 140));
                }else{
                    params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, dip2px(mContext, 100));
                }
            }else {
                params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            }

            holder.lineLayout.setLayoutParams(params);
            holder.view3.setVisibility(View.GONE);
            holder.view4.setVisibility(View.VISIBLE);
        } else {
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);
            holder.lineLayout.setLayoutParams(params);
            holder.view3.setVisibility(View.VISIBLE);
            holder.view4.setVisibility(View.GONE);
        }

        return convertView;
    }

    class ViewHolder {
        ImageView view1, view2, view3, view4;
        TextView tvTime, tvAddress;
        LinearLayout lineLayout;
        MyGridView gridView;
    }

    private static class AnimateFirstDisplayListener extends
            SimpleImageLoadingListener {

        static final List<String> displayedImages = Collections
                .synchronizedList(new LinkedList<String>());

        @Override
        public void onLoadingComplete(String imageUri, View view,
                                      Bitmap loadedImage) {
            if (loadedImage != null) {
                ImageView imageView = (ImageView) view;
                boolean firstDisplay = !displayedImages.contains(imageUri);
                if (firstDisplay) {
                    //FadeInBitmapDisplayer.animate(imageView, 500);
                    displayedImages.add(imageUri);
                }
            }
        }
    }

}
